import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;

public class JSONParser {
    
    public static String MapToJSON(Map<String, Object> map, String objName){
        
        String[] properties = new String[map.size()];
        int i = 0;
        for(Map.Entry<String, Object> entry : map.entrySet()){
            properties[i++] = "\r\n    \"" + entry.getKey() + "\" : \"" + entry.getValue() + "\"";
        }
        
        String JSON = "\"" + objName + "\" : {" + String.join(", ", properties) + " \r\n}";
        
    	return JSON;
    }
    
    public static String ListToJSON(List<Map<String, Object>> list, String objName){
        
        int i = 0;
        String[] objs = new String[list.size()];
        for(Map<String, Object> map : list){
            objs[i++] = "\r\n" + MapToJSON(map, "Obj_" + i);
        }
        
        String JSON = "\"" + objName + "\" : {" + String.join(", ", objs) + "\r\n}";
        return JSON;
    }
    
    public static Map<String, Object> JSONToMap(String objJSON){
        
        Map<String, Object> objMap = new HashMap<String, Object>();
        
        objJSON = objJSON.replace("{", "");
        objJSON = objJSON.replace("}", "");
        objJSON = objJSON.replace("\"", "");
        String[] objStrList = objJSON.split(",");
        
        for(String str : objStrList){
            String[] tmp = str.split(":");
            objMap.put(tmp[0].trim(), tmp[1].trim());
        }
        
        return objMap;
        
    }
    
    public static List<Map<String, Object>> JSONToList(String objJSON){
        
        List<Map<String, Object>> objList = new ArrayList<Map<String, Object>>();
        
        objJSON = objJSON.replace("[", "");
        objJSON = objJSON.replace("]", "");
        objJSON = objJSON.replaceAll("\\}.*,.*\\{", "} &&& {");
        System.out.println(objJSON);
        String[] objStrList = objJSON.split("&&&");
        
        for(String str : objStrList){
            objList.add(JSONToMap(str));
        }
        
        return objList;
    }
    
}
